<template>
  <div class="mod-user">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item label="内容">
        <el-input class="selectWidth" v-model="dataForm.key" placeholder="" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
      </el-form-item>
    </el-form>

    <div class="margin-bottom-sm">
      <el-button size="mini" type="primary" @click="addOrUpdateHandle()">新增</el-button>
    </div>
    <el-table ref="tableRef" :data="dataList" :height="height" :header-cell-style="tableHeaderStyle" border
              v-loading="dataListLoading"  style="width: 100%;">
      <el-table-column prop="content" header-align="center" align="center" label="内容">
      </el-table-column>
      <el-table-column prop="status" header-align="center" align="center" label="状态" width="150">
        <template slot-scope="scope">
          {{scope.row.status==1?'启用':'停用'}}
        </template>
      </el-table-column>
      <el-table-column fixed="right" header-align="center" align="center" width="150" label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
          <el-button type="text" size="small" @click="delData(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="pageIndex"
                   :page-sizes="[10, 20, 50, 100]" :page-size="pageSize" :total="totalPage"
                   layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <template-add-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></template-add-update>

  </div>
</template>

<script>
import TemplateAddUpdate from './msg-add-update.vue'
export default {
  data() {
    return {
      tableHeaderStyle: { background: '#f5f7fa !important', height: '40px !important', padding: '4px !important' },

      height: 0,
      dataForm: {
        key: '',
      },
      dataList: [],
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false,
      sendCodeVisible: false,
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,

    }
  },
  components: {
    TemplateAddUpdate
  },
  activated() {
    this.getDataList()
  },
  mounted() {
    console.log(document.body.clientHeight)
    // 至于减去多少，这个要看你的页面高度和你想将滚动轴固定在哪里了，数值可以更换
    this.$nextTick(() => {
      let tableHeight = document.body.clientHeight - 275
      this.height = tableHeight
    })
  },
  methods: {
    repairTable() {
      this.$nextTick(() => {
        // _self.$refs.你的el-table的ref.doLayout()
        this.$refs.tableRef.doLayout()
      })
    },
    // 发送短信
    sendCodeHandle (id) {
      this.sendCodeVisible = true
      this.$nextTick(() => {
        this.$refs.sendCode.init()
      })
    },
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true
      let search = {
        'key': this.dataForm.key,
      }
      this.$http({
        url: this.$http.adornUrl('/class/msg/list'),
        method: 'get',
        params: this.$http.adornParams(search)
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.dataList = data.page.list
          this.totalPage = data.page.totalCount
        } else {
          this.dataList = []
          this.totalPage = 0
        }
        this.repairTable()
        this.dataListLoading = false
      })
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val
      this.getDataList()
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val
    },
    // 新增 / 修改
    addOrUpdateHandle(id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id)
      })
    },
  }
}
</script>
